﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Qadir.MVC.Streamline;

namespace QadirMVCStreamlineWinFormsSample
{
    /// <summary>
    /// The defined flow for this program.
    /// </summary>
    public class ProgramFlow : Streamline
    {
        delegate void AllFormsClosed();

        event AllFormsClosed OnAllFormsClosed;

        public ProgramFlow()
            :base()
        {
            AddView(typeof(Form1));
            OnAllFormsClosed += ProgramFlow_OnAllFormsClosed;
        }

        void ProgramFlow_OnAllFormsClosed()
        {
            Application.Exit();
        }

        public override int StreamlineMain(object[] arguments = null)
        {
            ShowView("Form1", new Dictionary<string, object>() { { "Title", "Hello, world!" } });

            Qadir.Async.GoVoid(CheckForms, false);
            return 0;
        }

        void CheckForms()
        {
            while (true)
            {
                if (Application.OpenForms.Count < 1)
                {
                    OnAllFormsClosed();
                    break;
                }
            }
        }
    }
}
